var glo_$alert = $(".alert");
var glo_alertTem = true;//定时任务

function checkLogined() {
    $.ajax({
        url: "login/checkLogined.zb",
        type: "get",
        dataType: "JSON"
    }).done(function (res) {
        if (res) {
            window.location.href = "back/index.zb";
        }
    })
}

function formControlFocus() {
    $(".input-group i").removeClass("inputFocus")
    $(this).prev().find("i").addClass("inputFocus");
}

function formControlBlur() {
    $(".input-group i").removeClass("inputFocus")
    $(this).prev().find("i").addClass("inputFocus");
}

function bindSubmit(captchaObj) {
    $("#loginBtn").on('click', {captchaObj: captchaObj}, loginBtnClick);
    captchaObj.appendTo("#verifCode");
    captchaObj.onError(function () {
        glo_$alert.text("验证出错啦，请稍后重试！").slideDown();
    });
    captchaObj.onReady(function () {
        $("#verifWait").hide();
    });
}

function loginBtnClick(ev) {
    var param = checkParam();
    if (!param) {
        return false;
    }
    var result = ev.data.captchaObj.getValidate();
    if (!result) {
        glo_$alert.text("请滑动滑块进行验证！").slideDown();
        return false;
    }
    param.geetest_challenge = result.geetest_challenge;
    param.geetest_validate = result.geetest_validate;
    param.geetest_seccode = result.geetest_seccode;
    var $this = $(this);
    $this.text("登录中...");
    $.ajax({
        url: "login.zb",
        type: "POST",
        dataType: "JSON",
        contentType: "application/json",
        data: JSON.stringify(param)
    }).done(function (data) {
        if (data && data.success) {
            $this.text("登录成功，准备跳转...");
            glo_$alert.removeClass("alert-danger").addClass("alert-success").text("登录成功，准备跳转...").slideDown();
            window.location.href = "back/index.zb";
        } else {
            glo_$alert.text(data.msg).slideDown();
            ev.data.captchaObj.reset();
            $this.html("登&emsp;&emsp;录")
        }
    }).fail(function () {
        glo_$alert.text("系统异常，请联系管理员！").slideDown();
        ev.data.captchaObj.reset();
        $this.html("登&emsp;&emsp;录")
    })
}

function checkParam() {
    glo_$alert.empty();
    var passTem = true;
    var username = $("#username").val().trim();
    var password = $("#password").val().trim();
    if (!username) {
        glo_$alert.append(" 用户名不能为空！ ").slideDown();
        passTem = false;
    }
    if (!password) {
        glo_$alert.append(" 密码不能为空！ ").slideDown();
        passTem = false;
    }
    if (passTem) {
        var param = {};
        param.username = username;
        param.password = password;
        return param
    }
    return false;
}

function alertInterval() {
    if (glo_$alert.is(":visible") && glo_alertTem) {
        glo_alertTem = false;
        setTimeout(function () {
            glo_$alert.slideUp();
            glo_alertTem = true;
        }, 3000);
    }
}

function initCode() {
    //初始化验证
    $.ajax({
        url: "verifCode/initGeetest.zb?t=" + (new Date()).getTime(),
        type: "POST",
        dataType: "json",
        success: function (data) {
            setTimeout(function () {
                initGeetest({
                    gt: data.gt,
                    challenge: data.challenge,
                    new_captcha: data.new_captcha,
                    offline: !data.success,
                    product: "popup",
                    width: "100%"
                }, bindSubmit);
            })
        }
    });
}

function keyDownEvent(e) {
    var keyCode = e.keyCode;
    if (keyCode == 13) {
        $("#loginBtn").trigger("click");
        return false;
    }
}